package com.yc.thread;


public class ThreadDemo {
	
	public static void main(String[] args) throws InterruptedException {
		// 创建线程对象
		MyThread t1 = new MyThread();
		
		MyRunnable r1 = new MyRunnable();
		Thread t2 = new Thread(r1);
		
		// 启动线程
		t1.start();
		t2.start();
		
		/////////////////////////////////////////////////
		Thread t = Thread.currentThread();
		// 休眠 1秒
		Thread.sleep(1000);
		System.out.println(t.getId());
		System.out.println(t.getName());
		System.out.println(t.getPriority());
		
		// 使用匿名类创建线程 =》 继承
		Thread t3 = new Thread("我的线程") {
			@Override
			public void run() {
				Thread t = Thread.currentThread();
				System.out.println("=====================");
				System.out.println(t.getId());
				System.out.println(t.getName());
				System.out.println(t.getPriority());
				System.out.println("=====================");
			}
		};
		t3.start();
		
		System.out.println("=========== 线程优先级 ==========");
		
		Thread t4 = new MyThread();
		Thread t5 = new Thread(new MyRunnable());
		// 优先级必须在启动前设置
		t5.setPriority(8);
		// 精灵线程
		t4.setDaemon(true);
		t5.setDaemon(true);
		t4.start();
		t5.start();
		
		System.out.println("=============== 主线程代码执行结束 =================");
		
		
	}

}

class MyThread extends Thread{
	@Override
	public void run() {
		// 线程启动后执行的任务方法
		for (int i = 0; i < 100; i++) {
			System.out.println("扫地 " + i);
		}
	}
}

class MyRunnable implements Runnable{

	@Override
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println("擦窗 " + i);
		}
	}
	
}

